<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" :title="isAdd ? '新增仓库' : '修改仓库'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="160px">
      <el-form-item label="仓库编码" prop="busMainData.thirdCode">
        <el-input v-model="thirdCode" :readonly="!isAdd" style="width: 300px;" />
      </el-form-item>
      <el-form-item label="名称" prop="busMainData.warehouseName">
        <el-input v-model="form.busMainData.warehouseName" style="width: 300px;"/>
      </el-form-item>
      <el-form-item label="仓库类别" prop="busMainData.warehouseType">
        <el-select v-model="form.busMainData.warehouseType" :style="style" placeholder="请选类别">
          <el-option
            v-for="(item) in dicts.warehouse_type"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="仓库位置" prop="busMainData.warehouseLoc">
        <el-input v-model="form.busMainData.warehouseLoc" style="width: 300px;"/>
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;" label="上级仓库" prop="busMainData.pid">
        <WarehouseTreeSelect v-model="form.busMainData.pid" :cur-id="form.busMainData.id" value-field="id" style="width: 300px;" placeholder="选择仓库" />
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','WAREHOUSE_ALL','WAREHOUSE_CREATE']" :loading="loading" type="primary" @click="doSubmit">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, viewMainData, toAdd, toCopy } from '@/api/base/warehouse'
import WarehouseTreeSelect from '@/views/base/warehouse/module/WarehouseTreeSelect'

export default {
  components: { WarehouseTreeSelect },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  dictTypes: ['warehouse_type'],
  data() {
    return {
      isAdd: false,
      loading: false,
      dialog: false,
      style: 'width: 184px',
      form: {
        busMainData: {
          id: '',
          pid: '',
          thirdCode: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
        }
      },
      rules: {
        'busMainData.thirdCode': [
          { required: true, message: '请输入编号', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        'busMainData.warehouseName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.warehouseType': [
          { required: true, message: '请选择类别', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    thirdCode: {
      get: function() {
        return this.form.busMainData.thirdCode
      },
      set: function(val) {
        this.form.busMainData.thirdCode = val.toUpperCase()
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    addData(pid) {
      this.isAdd = true
      const data = { paramBean: { }, busMainData: { pid: pid }}
      toAdd(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      toCopy(data).then(res => {
        this.dialog = true
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      this.dialog = true
      const data = { paramBean: { id: id }}
      viewMainData(data).then(res => {
        this.form = res.data
      })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>
